<template>
  <div>
    <el-container>
      <el-aside class="aside-box" width="12vw">
        <div class="logo" @click="switchRoute('/', 0)">
          <img src="/logo.png" alt="logo" />
          <span>宠喵</span>
        </div>

        <div class="nav">
          <div v-for="(route, index) in routesInfo" :key="index" class="item" @click="switchRoute(route.path, index)">
            <i class="iconfont" :class="route.meta.icon"
              :style="{ color: currentIndex === index ? '#6C73F5' : '#949aaa' }" />
            <div class="title" :style="{ color: currentIndex === index ? '#273556' : '#949aaa' }">
              {{ route.meta.title }}
            </div>
            <div class="separate" :style="{ backgroundColor: currentIndex === index ? '#8C91F4' : '#fff' }">&nbsp;</div>
          </div>
        </div>

        <div class="logout">
          <el-button link @click="logout"><i class="iconfont icon-logout" />退出登录</el-button>
        </div>
      </el-aside>

      <el-main class="main-box">
        <RouterView />
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
import { useChildIndexRoutes } from '@/util/get_index';
const router = useRouter();
// 获取路由信息, 初始化导航栏
const routesInfo = useChildIndexRoutes();
const currentIndex = ref(0);
// 刷新不丢失当前路由
onMounted(() => {
  const index = routesInfo.value.findIndex((route) => route.path === router.currentRoute.value.path);
  currentIndex.value = index;
});
// 切换页面
const switchRoute = (path, index) => {
  currentIndex.value = index;
  router.push(path);
};
// 退出登录
import { useTokenStore } from '@/stores/token';
const tokenStore = useTokenStore();
const logout = () => {
  tokenStore.removeToken();
  router.push('/login');
};
</script>

<style scoped lang="scss">
.aside-box {
  .logo {
    height: 7vh;
    margin: 5vh auto;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;

    img {
      width: 28%;
      margin-right: 6px;
    }

    span {
      font-size: 18px;
      font-weight: bold;
    }
  }

  .nav {
    height: 70vh;

    .item {
      height: 40px;
      position: relative;
      margin: 8px 0;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;

      .title {
        width: 4vw;
        text-align: center;
      }

      .separate {
        position: absolute;
        right: 0;
        width: 5px;
        height: 16px;
      }
    }
  }

  .logout {
    height: 13vh;
    text-align: center;
  }
}

.main-box {
  position: relative;
  width: 88vw;
  background-color: #f5f5fb;
  --el-main-padding: 2vmax 2vmax 0 2vmax;
}

.iconfont {
  font-size: 18px;
  margin-right: 12px;
}
</style>